package com.sskj.fabi.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.PayWayBean;
import com.sskj.fabi.bean.SysConfigBean;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.activity.PublishActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.http.JsonCallBack;

import java.util.List;

import io.reactivex.Flowable;


public class PublishActivityPresenter extends BasePresenter<PublishActivity> {
    /**
     * 获取用户支付方式
     */
    public void requsetPayWayList() {
        httpService.getPayList()
                .execute(new JsonCallBack<HttpData<List<PayWayBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<List<PayWayBean>>> response) {
                        mView.onPayWaySuccess(response.body().getData());
                    }

                    @Override
                    public void onError(Response<HttpData<List<PayWayBean>>> response) {
                        super.onError(response);
                    }
                });
    }

    /**
     *
     */
    public void requsetPublish(String type, String sumCount, String lowCount,
                               String hightCount, String unitPrice, String payTypeRecord, String pwd) {
        httpService.createFabiOrder(type, sumCount, lowCount, hightCount, unitPrice, payTypeRecord, pwd)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == HttpConfig.OK) {
                            RxBus.getDefault().send(RxBusCode.FABI_DATING_REFRESH);
                            mView.finish();
                        }
                    }
                });

    }


    /**
     * 获取估价
     */
    public void requestUnitPrice(boolean isBuy) {  // flag t-->true买入   false卖出
        httpService.getSysConfig(3)
                .execute(new JsonCallBack<HttpData<List<SysConfigBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<SysConfigBean>>> response) {
                        Flowable.fromIterable(response.body().getData())
                                .filter(item -> "ck_price".equals(item.getName()) || "ck_sell_price".equals(item.getName()))
                                .subscribe(item -> {
                                    if ("ck_price".equals(item.getName())){
                                        mView.updateBuyPrice(item.getValue());
                                    }else if ("ck_sell_price".equals(item.getName())){
                                        mView.updateSellPrice(item.getValue());

                                    }
                                });
                    }
                });

    }

}
